.org 0xC582C ; Piercing Vision
	
	MOV		R0, R1
	SUB		R0, R0, R2
	MOV		R0, R0, LSL 16
	MOV		R8, R0, ASR 16
	LDR		R0, [SP, 0x4]
	RSB		R1, R2, 0
	ADD		R0, R0, R2
	MOV		R1, R1, LSL 16
	STR		R0, [SP, 0x14]
	CMP		R8, R0
	MOV		R7, R1, ASR 16
	ADDGT	SP, SP, 0x24
	
	POPGT	{R4-R11, LR}
	BXGT	LR
	
	LDRSB	R0, [SP, 0x48]
	LDRB	R3, [SP, 0x4C]
	MOV		R1, 0x98
	STR		R0, [SP, 0x10]
	MUL		R0, R3, R1
	STR		R0, [SP, 0xC]
	LDR		R0, [SP, 0x0]
	LDR		R5, [Piercing_Vision_1]
	SUB		R0, R0, R2
	STR		R0, [SP, 0x8]
	LDR		R0, [SP, 0x0]
	LDR		R4, [Piercing_Vision_2]
	ADD		R11, R0, R2

	MOV 	R0, CO_Piercing_Vision				; Skill: Piercing Vision

	STR		R0, [SP, 0x1C]
	
Piercing_Vision_Branch_5:
	
	CMP		R8, 0
	BLT		Piercing_Vision_Branch_2
	
	LDR		R1, [R5, 0]
	LDR		R0, [R4, 0]
	ADD		R0, R1, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	LDR 	R0, [R0, 0x1C4]
	LDRH	R0, [R0, 0x1C]
	CMP		R8, R0
	
	BGE		Piercing_Vision_Branch_2
	
	MOV		R0, R7
	
	BL		0x1220AC
	
	LDR		R1, [SP, 0x8]
	ADD		R0, R1, R0
	MOV		R0, R0, LSL 16
	MOV		R9, R0, ASR 16
	MOV		R0, R7
	
	BL		0x1220AC
	
	SUB		R0, R11, R0
	CMP		R9, R0
	
	BGT		Piercing_Vision_Branch_2
	
	LDR		R0, [SP, 0x4]
	SUB		R6, R8, R0
	RSB		R0, R6, 0
	STR		R0, [SP, 0x18]

Piercing_Vision_Branch_4:
	
	CMP		R9, 0
	BLT		Piercing_Vision_Branch_1
	
	LDR		R1, [R5, 0]
	LDR		R0, [R4, 0]
	ADD		R0, R1, R0, LSL 2
	LDR		R1, [R0, 0x48]
	LDR		R0, [R1, 0x1C4]
	LDRH	R0, [R0, 0x1A]
	CMP		R9, R0
	
	BGE		Piercing_Vision_Branch_1
	
	ADD		R0, R1, 0x5000
	LDR		R2, [R0, 0xA58]
	LDR		R0, [SP, 0xC]
	LDR		R1, [SP, 0x1C]
	ADD		R0, R2, R0
	ADD		R0, R0, 0x74
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped
	CMP 	R0, 0								; Does CO use that skill?
	BNE		CO_Has_Piercing_Vision	

	CMP 	R6, 0
	LDRLT	R1, [SP, 0x18]
	LDR		R0, [SP, 0x0]
	MOVGE	R1, R6
	SUBS	R0, R9, R0
	RSBMI	R0, R0, 0
	ADD		R0, R0, R1
	CMP		R0, 1	
	
	BLE		CO_Has_Piercing_Vision
	
	LDR		R1, [R5, 0]
	LDR		R0, [R4, 0]
	ADD		R0, R1, R0, LSL 2
	LDR		R1, [R0, 0x48]
	ADD		R0, R1, 0x4000
	LDR		R0, [R0, 0x3D0]
	ADD		R2, R0, R8, LSL 1
	ADD		R2, R2, 0x4100
	LDRH	R2, [R2, 0x68]
	ADD		R2, R9, R2
	ADD		R2, R0, R2
	ADD		R0, R2, 0x1000
	LDRB	R0, [R0, 0x420]
	AND		R0, R0, 0x1F
	
	CMP		R0, Terrain_Woods					; 	Is terrain Wood?
	BEQ		Vision_Terrain_Check
	
	CMP		R0, Terrain_Reef					; 	Is terrain Reef?
	BNE		CO_Has_Piercing_Vision

;-------------------------------------------------------------------------------

Vision_Terrain_Check:

	LDRB	R0, [R2, 0x508]
	CMP		R0, 0
	
	BEQ		Piercing_Vision_Branch_1
	
	ADD		R1, R1, 0x5000
	LDR		R2, [R1, 0x75C]
	MOV		R1, 0xE
	MUL		R1, R0, R1
	ADD		R0, R2, R1
	LDRB	R1, [R2, R1]
	LDR		R2, [Piercing_Vision_3]
	MOV		R3, 0x6C
	MLA		R2, R1, R3, R2
	LDRB	R2, [R2, 0x1C]
	
	CMP		R2, 3
	BEQ		Piercing_Vision_Branch_3
	
	CMP		R2, 4
	BNE		Piercing_Vision_Branch_1

Piercing_Vision_Branch_3:
	
	CMP		R1, 12
	BNE		CO_Has_Piercing_Vision
	
	LDRB	R1, [R0, 0x1]
	ANDS	R1, R1, 0x20
	BEQ		CO_Has_Piercing_Vision
	
	LDRB	R1, [R0, 0x2]
	LDRB	R2, [R0, 0x3]
	
	BL		0xC903C
	
	CMP		R0, 0
	BEQ		Piercing_Vision_Branch_1
	
;-------------------------------------------------------------------------------

CO_Has_Piercing_Vision:

	LDR		R1, [R5, 0]
	LDR		R0, [R4, 0]
	ADD		R0, R1, R0, LSL 2
	LDR		R0, [R0, 0x48]
	ADD		R0, R0, 0x4000
	LDR		R0, [R0, 0x3D0]
	ADD		R0, R0, R8, LSL 1
	ADD		R0, R0, 0x4100	
	LDRH	R0, [R0, 0x68]
	ADD		R2, R9, R0
	LDRSB	R1, [R10, R2]
	LDR		R0, [SP, 0x10]	
	ADD		R0, R1, R0
	STRB	R0, [R10, R2]

Piercing_Vision_Branch_1:  

	ADD		R0, R9, 1
	MOV		R0, R0, LSL 16
	MOV		R9, R0, ASR 16
	MOV		R0, R7
	
	BL		0x1220AC
	
	SUB		R0, R11, R0
	
	CMP		R9, R0
	BLE		Piercing_Vision_Branch_4

Piercing_Vision_Branch_2:

	ADD		R0, R8, 1
	MOV		R0, R0, LSL 16
	MOV		R8, R0, ASR 16
	LDR		R0, [SP, 0x14]
	CMP		R8, R0
	ADD		R0, R7, 1
	MOV		R0, R0, LSL 16
	MOV		R7, R0, ASR 16	
	
	BLE		Piercing_Vision_Branch_5
	
	ADD		SP, SP, 0x24
	POP		{R4-R11, LR}
	BX 		LR
	
;-------------------------------------------------------------------------------

Piercing_Vision_1:
	.dw Reg1_Skill
	
Piercing_Vision_2:
	.dw Reg2_Skill

Piercing_Vision_3:
	.dw 0x022F4FB8

